package com.jt.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * <p>
 *
 * </p>
 *
 * @author Song
 * @since 2020/11/12
 */
public class OjbectMapperUtil {
    private static final ObjectMapper MAPPER = new ObjectMapper();

    public static String toJson(Object target){
        if (target==null){
            throw new RuntimeException("参数不可为空");
        }
        try {
            return MAPPER.writeValueAsString(target);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

    public static <T> T toOjbect(String json,Class<T> clazz){
        if (json==null||clazz==null){
            throw new RuntimeException("参数不可为空");
        }
        try {
           return MAPPER.readValue(json, clazz);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
